package com.feng.gaoerfu.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.feng.gaoerfu.R;
import com.feng.gaoerfu.base.App;
import com.feng.gaoerfu.base.BaseActivity;
import com.feng.gaoerfu.bean.BallPersonData;
import com.feng.gaoerfu.view.framework.picker.DateTimePicker;
import com.feng.gaoerfu.view.framework.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;
import butterknife.OnClick;

import static com.feng.gaoerfu.activity.MyBallFriendActivity.COME_FROM_FRIEND;

/**
 * @time 2017/8/12 0012 19:39
 */

public class ScoringNowActivity extends BaseActivity {
    private static final int REQUEST_FRIEND = 1001;
    @InjectView(R.id.tv_back)
    TextView mTvBack;
    @InjectView(R.id.tv_title)
    TextView mTvTitle;
    @InjectView(R.id.iv_menu)
    ImageView mIvMenu;
    @InjectView(R.id.recycler)
    RecyclerView mRecycler;
    @InjectView(R.id.ll_time)
    LinearLayout mLlTime;
    @InjectView(R.id.tv_next)
    TextView mTvNext;
    @InjectView(R.id.edt_address)
    EditText mEdtAddress;
    @InjectView(R.id.edt_game_name)
    EditText mEdtGameName;
    @InjectView(R.id.layout_open_time)
    RelativeLayout mLayoutTime;
    @InjectView(R.id.tv_open_time)
    TextView mTvTime;
    private ArrayList<BallPersonData> mBallPersonList;
    private MyAdapter mAdapter;


    @Override
    public int initContentView() {
        return R.layout.activity_scoring_now;
    }

    @Override
    public void initView() {

        mTvTitle.setText("立即记分");
        mIvMenu.setVisibility(View.GONE);
        mEdtAddress.setSelection(mEdtAddress.getText().length());
    }

    @Override
    public void initData() {
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        mBallPersonList = new ArrayList<>();
        mBallPersonList.add(new BallPersonData(BallPersonData.ADD));
        mBallPersonList.add(new BallPersonData(BallPersonData.ADD));
        mBallPersonList.add(new BallPersonData(BallPersonData.ADD));
        mBallPersonList.add(new BallPersonData(BallPersonData.ADD));

        mAdapter = new MyAdapter(R.layout.item_person, mBallPersonList);
        mRecycler.setAdapter(mAdapter);
    }

    class MyAdapter extends BaseQuickAdapter<BallPersonData> implements View.OnClickListener {

        public MyAdapter(int layoutResId, List<BallPersonData> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, BallPersonData data) {
            View addView = helper.getView(R.id.item_add_view);
            View personView = helper.getView(R.id.item_view_person);
            if (data.type == BallPersonData.ADD) {
                addView.setVisibility(View.VISIBLE);
                personView.setVisibility(View.GONE);
            } else {
                addView.setVisibility(View.GONE);
                personView.setVisibility(View.VISIBLE);
                helper.setImageResource(R.id.item_iv, data.drawable)
                        .setText(R.id.item_tv, data.name);
            }

            addView.setOnClickListener(this);
            personView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            LogUtils.debug(TAG, "click");
            Intent intent = new Intent(mContext, MyBallFriendActivity.class);
            intent.putExtra(COME_FROM_SCORING, JSONArray.toJSONString(mBallPersonList));
            toActivity(intent, REQUEST_FRIEND);
        }
    }

    public static final String COME_FROM_SCORING = "listFromScoring";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_FRIEND) {
            ArrayList<BallPersonData> list = data.getParcelableArrayListExtra(COME_FROM_FRIEND);
            //清空集合把选中的对象添加进去后补全未添加的
            mBallPersonList.clear();
            mBallPersonList.addAll(list);
            for (int i = 0; i < 4 - list.size(); i++) {
                mBallPersonList.add(new BallPersonData(BallPersonData.ADD));
            }
            mAdapter.setNewData(mBallPersonList);
        }
    }

    @OnClick({R.id.tv_back, R.id.tv_next, R.id.layout_open_time})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_next:
                next();
                break;
            case R.id.layout_open_time:
                openTime();
                break;
        }
    }

    private void openTime() {
        DateTimePicker picker = new DateTimePicker(this, DateTimePicker.HOUR_24);
        picker.setDateRangeStart(2017, 1, 1);
        picker.setDateRangeEnd(2025, 11, 11);
        picker.setTimeRangeStart(9, 0);
        picker.setTimeRangeEnd(20, 30);
        picker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
            @Override
            public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
                App.mOpenTime = year + "-" + month + "-" + day + " " + hour + ":" + minute;
                mTvTime.setText(App.mOpenTime);
            }
        });
        picker.show();
    }

    public static final String TAG = "ScoringNowActivity";

    private void next() {
        String address = mEdtAddress.getText().toString();


        //给app中的球员集合赋值,并且清空数据
        App.mPersonList.clear();
        App.mTableLists.clear();

        for (int i = 0; i < mBallPersonList.size(); i++) {
            if (mBallPersonList.get(i).type != BallPersonData.ADD) {
                App.mPersonList.add(mBallPersonList.get(i));
            }
        }
        Log.d(TAG, "next  App.mPersonList = " +  App.mPersonList);
        if (TextUtils.isEmpty(address)) {
            showShortToast("请输入球场名称");
            return;
        }
        App.mBallCourt = address;
        if (TextUtils.isEmpty(App.mOpenTime)) {
            showShortToast("请选择开球时间");
            return;
        }
        if (App.mPersonList.size() >= 1) {
            toActivity(new Intent(mContext, MainActivity.class));
        } else {
            showShortToast("请至少添加一个球员");
        }
    }


}
